﻿using System.Linq;
using Magiq.Delete;
using Magiq.Insert;
using Magiq.Update;

namespace Magiq.Objects {
  public class MagiqToObjects : IMagiqProvider, IUpdateProvider, IDeleteProvider, IInsertProvider {
    #region IDeleteProvider Members

    public int Execute<T>(IQueryableDelete<T> delete) where T : class {
      return new EnumerableDelete<T>(delete.Where).Execute();
    }

    #endregion

    #region IInsertProvider Members

    public int Execute<F, T>(IQueryableInsert<F, T> insert)where T : class {
      return new EnumerableInsert<F, T>(insert.Source, insert.NewExpression).Into(insert.Destination);
    }

    #endregion

    #region IMagiqProvider Members

    public IUpdateProvider GetUpdateProvider() {
      return this;
    }

    public IDeleteProvider GetDeleteProvider() {
      return this;
    }

    public IInsertProvider GetInsertProvider() {
      return this;
    }

    public bool Handles(IQueryable queryable) {
      return true;
    }

    #endregion

    #region IUpdateProvider Members

    public int Execute<T>(IQueryableUpdate<T> update) where T : class {
      return new EnumerableUpdate<T>(update.Where, update.PropertySets).Update();
    }

    #endregion
  }
}